<?php

class Core_Model_Website_Menu extends Dcmp_Model_Entity {

    public function __construct() {
        parent::__construct();
    }

    protected function getTableClass() {
        return Kd_Loader::loadClass('table_website_menu');
    }

    protected function beforeInsert($transaction) {
        $parent = new self();
        $sefurl = $this->getSefUrl();
        if (empty($sefurl)) {
            $this->setSefUrl('/global/' . $this->getName());
        }

        if ($this->getParentId() && $this->getParentId() != -1) {
            $idPath = $parent->load($this->getParentId())->getIdPath();
            $this->setIdPath($idPath);
        } else {
            $this->setParentId(0);
            $this->setIdPath('');
        }
        $this->setLevel($parent->getLevel() + 1);
        $sorting = $this->getTable()->getMaxSorting($this->getParentId()) + 1;
        $this->setSorting($sorting);
        parent::beforeInsert($transaction);
    }

    protected function afterInsert($transaction) {
        Kd_Data_Factory::getUpdateDcl()
        ->into('{t}website_menu')
                ->value('id_path',':id_path:s')
                ->where()->eq('id',':id:i')
                ->closest()->addParameter(array('id_path'=>$this->getIdPath().$this->getId().'_','id'=>$this->getId()))
                ->execute();
        $sefurl = $this->getSefUrl();
        if (empty($sefurl)) {
            $sefurl = '/global/' . $this->getName();
        }
        App::getHelper('website')->addSefUrl('global', $this->getId(), '/global/menu/id/' . $this->getId(), $sefurl);
        parent::afterInsert($transaction);
    }

    protected function beforeUpdate($transaction) {
        $parent = new self();
        if ($this->propertyIsChanged('parent_id')) {
            $sorting = $this->getTable()->getMaxSorting($this->getParentId()) + 1;
            $this->setSorting($sorting);
            if ($this->getParentId() && $this->getParentId() == -1) {
                $this->setParentId(0);
                $this->setIdPath($this->getId() . '_');
            } else {
                $idPath = $parent->load($this->getParentId())->getIdPath() . $this->getId() . '_';
                $this->setIdPath($idPath);
            }

            $this->setLevel($parent->getLevel() + 1);
            //            $this->setSorting($sorting);
            //            if (!$this->propertyIsChanged('sorting')) {
            //                $this->changedProperties[] = 'sorting';
            //            }
        }

        parent::beforeUpdate($transaction);
    }

    protected function afterUpdate($transaction) {
        if ($this->propertyIsChanged('sef_url')) {
            App::getHelper('website')->deleteSefUrl('global', $this->getId())->addSefUrl('global', $this->getId(), '/website/menu/id/' . $this->getId(), $this->getSefUrl());
        }
        if ($this->propertyIsChanged('parent_id')) {
            $this->getTable()->updateIdPath($this->getOriginalData('id_path'), $this->getIdPath(), $transaction);
        }
        parent::afterUpdate($transaction);
    }

    protected function initializeTable($table) {
        $table->setName('{t}website_menu');
        $table->createColumn('id', 'integer', TRUE, TRUE);
        $table->createColumn('name', 'string');
        $table->createColumn('url', 'string');
        $table->createColumn('sef_url', 'string');
        $table->createColumn('parent_id', 'integer');
        $table->createColumn('id_path', 'string');
        $table->createColumn('enabled', 'integer');
        $table->createColumn('area_id', 'integer');
        $table->createColumn('level', 'integer');
        $table->createColumn('sorting', 'integer');
        $table->createColumn('creator_id', 'integer');
        $table->createColumn('created_time', 'datetime')->setDefaultValue(date('Y-m-d H:i:s'));
    }

    public function getChildren() {
        return App::getModel('website/collection_menu')
                ->where()
                ->eq('parent_id', $this->getId())
                ->closest()
                ->load();
    }

}